{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import threading\n",
    "import queue\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "in_queue = queue.Queue()\n",
    "out_queue = queue.Queue()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def square_threading():\n",
    "    while True:\n",
    "        n = in_queue.get()\n",
    "        if n == 'STOP':\n",
    "            return\n",
    "        n_squared = n**2\n",
    "        out_queue.put(n_squared)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "thread = threading.Thread(target=square_threading)\n",
    "thread.start()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 squared is 0\n",
      "1 squared is 1\n",
      "2 squared is 4\n",
      "3 squared is 9\n",
      "4 squared is 16\n",
      "5 squared is 25\n",
      "6 squared is 36\n",
      "7 squared is 49\n",
      "8 squared is 64\n",
      "9 squared is 81\n"
     ]
    }
   ],
   "source": [
    "for i in range(10):\n",
    "    in_queue.put(i)\n",
    "    i_squared = out_queue.get()\n",
    "    print(f\"{i} squared is {i_squared}\")\n",
    "in_queue.put('STOP')\n",
    "thread.join()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
